package zdream.rockchronicle.profiles.megaman;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import zdream.control.assets.TextureDescription;
import zdream.control.assets.TextureDescription.Region;
import zdream.control.profile.mm.MegaManProfile;
import zdream.control.world.Ticker;
import zdream.rockchronicle.RockChronicle;
import zdream.rockchronicle.core.profile.HealthMiscPainter;

/**
 * 来画前景的 painter, 包括血条、暂停界面内容等
 * 
 * @author Zdream
 */
public class MegaManHealthMiscPainter extends HealthMiscPainter {
	public final MegaManProfile owner;

	public MegaManHealthMiscPainter(MegaManProfile owner) {
		this.owner = owner;
		app = RockChronicle.INSTANCE;
		batch = app.graphics.uiBatch;
		initTextures();
	}

	Sprite bg;
	Sprite black;
	Sprite healthRed;
	TextureRegion health;
	TextureRegion fullHealth;

	TextureDescription textures;
	RockChronicle app;
	SpriteBatch batch;

	/*
	 * 绘制参数
	 */
	// p1 血条的位置
	public static final int P1_HP_BAR_X = 32, P1_HP_BAR_X_WHEN_PROFILE_OPEN = 262;
	public int P1_HP_BAR_Y;
	float p1HpBarX = P1_HP_BAR_X;
	float p1HpBarY;

	/**
	 * 处理的 paths 比如 ["res/foes/megaman/sprites/megaman7sheet.json"]
	 */
	private void initTextures() {
		P1_HP_BAR_Y = app.height - 48;
		p1HpBarY = P1_HP_BAR_Y;

		textures = app.assets.getDescription("res/misc/base_misc_sheet.json");

		Region entry = textures.findRegion("health_misc");
		bg = new Sprite();
		bg.setRegion(app.assets.findTextureRegion(entry));

		entry = textures.findRegion("black");
		black = new Sprite();
		black.setRegion(app.assets.findTextureRegion(entry));

		health = new TextureRegion();
		fullHealth = app.assets.findTextureRegion(textures.findRegion("health"));

		entry = textures.findRegion("health_red");
		healthRed = new Sprite();
		healthRed.setRegion(app.assets.findTextureRegion(entry));
	}

	@Override
	public void draw(Ticker ticker) {
		Region entry;

		// 所有绘画部分大小 x2
		entry = textures.findRegion("health_misc");
		float xStart = p1HpBarX;
		float yStart = p1HpBarY - entry.height * 2;

		int realHp = Math.max(owner.hp, 0);
		int hp = Math.min(realHp / 256, 28);
		boolean hpPlus = realHp % 256 != 0;
		if (hp == 28) {
			hpPlus = false;
		}

		batch.begin();
		// 首先画底
		batch.draw(bg, xStart, yStart,
				entry.width * 2, entry.height * 2);

		// 黑底
		entry = textures.findRegion("black");
		batch.draw(black, xStart + 14, yStart + 46,
				entry.width * 2, entry.height * 28 * 2);

		// 血条
		entry = textures.findRegion("health");
		health.setRegion(fullHealth, 0, entry.height - hp * 3, entry.width, hp * 3);
		batch.draw(health, xStart + 14, yStart + 46, entry.width * 2, hp * 6);
		if (hpPlus) {
			entry = textures.findRegion("health_red");
			batch.draw(healthRed, xStart + 14, yStart + 46 + hp * 6,
					entry.width * 2, entry.height * 2);
		}

		batch.end();
	}

	@Override
	public void dispose() {
		// do-nothing
	}

	@Override
	public float getX() {
		return p1HpBarX;
	}

	@Override
	public float getY() {
		return p1HpBarY;
	}

	@Override
	public void setX(float x) {
		this.p1HpBarX = x;
	}

	@Override
	public void setY(float y) {
		this.p1HpBarY = y;
	}
}
